//app.js
App({
    onLaunch: function () {
        //调用API从本地缓存中获取数据
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
    },
    getUserInfo: function (cb) {
        var that = this
        if (this.globalData.userInfo) {
            typeof cb == "function" && cb(this.globalData.userInfo)
        } else {
            //调用登录接口
            wx.login({
                success: function (res) {
                    wx.request({
                        url: 'https://mb.800151.com/phalapi/Public/applet/?service=WechatMini_WXLoginHelper.CheckLogin',
                        data: {
                            code: res.code
                        },
                        success: function (res) {

                            var tres = res;
                            wx.getUserInfo({
                                success: function (res) {
                                    console.log(res)
                                    that.globalData.userInfo = res.userInfo;
                                    typeof cb == "function" && cb(that.globalData.userInfo);

                                    var data = tres.data;
                                    if (data.ret == 200 && data.data.code == 0) {
                                        console.log(that.globalData.userInfo)
                                        console.log("openid = " + data.data.openid)
                                        console.log("session3rd = " + data.data.session3rd)
                                        that.globalData.openID = data.data.openid
                                        typeof cb == "function" && cb(that.globalData.openID)

                                        wx.request({
                                            url: 'https://mb.800151.com/phalapi/Public/applet/?service=demo.addOpenID',
                                            data: {
                                                openid: data.data.openid,
                                                nickname:that.globalData.userInfo.nickName,
                                                avatar:that.globalData.userInfo.avatarUrl,
                                                city:that.globalData.userInfo.city,
                                                gender:that.globalData.userInfo.gender
                                            },
                                            success: function (res) {
                                                // console.log(that.globalData.userInfo)
                                                // console.log(res)
                                            }
                                        })
                                    } else {
                                        console.log("登录失败")
                                        console.log("code = " + data.data.code)
                                        console.log("message = " + data.data.message)
                                    }
                                }
                            });

                            // console.log(that.globalData.userInfo );




                            // var data = tres.data;
                            // if (data.ret == 200 && data.data.code == 0) {
                            //
                            //     // console.log("openid = " + data.data.openid)
                            //     // console.log("session3rd = " + data.data.session3rd)
                            //     that.globalData.openID = data.data.openid
                            //     typeof cb == "function" && cb(that.globalData.openID)
                            //
                            //     wx.request({
                            //         url: 'https://mb.800151.com/phalapi/Public/applet/?service=demo.addOpenID',
                            //         data: {
                            //             openid: data.data.openid,
                            //             userInfo: that.globalData.userInfo
                            //         },
                            //         success: function (res) {
                            //             // console.log(that.globalData.userInfo)
                            //             // console.log(res)
                            //         }
                            //     })
                            // } else {
                            //     // console.log("登录失败")
                            //     // console.log("code = " + data.data.code)
                            //     // console.log("message = " + data.data.message)
                            // }
                        }
                    });
                }
            })
        }
    },
    globalData: {
        userInfo: null
    }
})